package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"mygo/app/models"
	"mygo/app/services"
	"mygo/app/tools"
)

type czx_ctl struct {
	db *gorm.DB
}

var Czx = czx_ctl{}

// getRcData
func (ctl *czx_ctl) GetRcData(c *gin.Context) {
	ctl.db = models.Db
	var hdrc []models.Hdrc
	dateStart := c.Query("dateStart")
	dateEnd := c.Query("dateEnd")
	fmt.Println(dateStart, dateEnd)
	ctl.db.Where("date >= ? AND date <= ?", dateStart, dateEnd).Find(&hdrc)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "success",
		"data": hdrc,
	})
}

// getXianData
func (ctl *czx_ctl) GetXianData(c *gin.Context) {
	ctl.db = models.Db
	code := c.Query("code")
	foreList := services.GetForeListByCode(code, ctl.db)
	var foreDetail models.ForeDetailJson
	//获取根据time排序的最新的一条数据
	if ctl.db.Where("foreListId = ? AND state != ?", foreList.Id, "0").Order("time desc").First(&foreDetail).RowsAffected == 0 {
		c.JSON(200, gin.H{
			"code":    0,
			"message": "Fail",
			"data":    nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code":    200,
		"message": "Success",
		"data":    foreDetail,
	})
}

// "YULIAO":  "玉寮",
// "AOJIANG": "鳌江",
// "DONGTOU": "洞头",
// "LONGWAN": "龙湾",
// "RUIAN":   "瑞安",
// "SGTOU":   "沙港头",
// "SHIPENG": "石砰",
// "WENZHOU": "温州",
// "DAMEND":  "大门",
// "NANJI":   "南麂",
var offsetMap = map[string]float64{
	"玉寮":  -164,
	"鳌江":  -186,
	"洞头":  -333,
	"龙湾":  -184,
	"瑞安":  -186,
	"沙港头": -100,
	"石砰":  -182,
	"温州":  -180,
	"大门":  -0,
	"南麂":  -0,
	"北麂":  -0,
	//"玉寮":  -0,
	//"鳌江":  -0,
	//"洞头":  -0,
	//"龙湾":  -0,
	//"瑞安":  -0,
	//"沙港头": -0,
	//"石砰":  -0,
	//"温州":  -0,
	//"大门":  -0,
	//"南麂":  -0,
	//"北麂":  -0,
}

//    let data=[
//      {
//        name:'',
//        data:[
//          {
//            date:'',
//            tideHighLow:[
//              {
//                time:'',
//                tide:''
//              }
//            ],
//            tideHour:{}
//          }
//        ]
//      }
//    ]

type GangkouTide struct {
	Time string `json:"time"`
	Tide string `json:"tide"`
}

type GangkouData struct {
	Name string `json:"name"`
	Data []struct {
		Date        string         `json:"date"`
		TideHighLow models.TwcTide `json:"tideHighLow"`
		TideHour    []GangkouTide  `json:"tideHour"`
	} `json:"data"`
}

// getGangkouData
func (ctl *czx_ctl) GetGangkouData(c *gin.Context) {
	ctl.db = models.Db
	dateStart := c.Query("dateStart")
	dateEnd := c.Query("dateEnd")
	//获取间隔了多少天
	dayGap := tools.GetDayGap(dateStart, dateEnd)
	//获取所有港口的潮汐数据
	var gangkouData []GangkouData
	for k, v := range offsetMap {
		var gangkou GangkouData
		gangkou.Name = k

		for i := 0; i < dayGap; i++ {
			var dataItem struct {
				Date        string         `json:"date"`
				TideHighLow models.TwcTide `json:"tideHighLow"`
				TideHour    []GangkouTide  `json:"tideHour"`
			}
			//var tideHourTemp GangkouTide
			//获取日期
			date := tools.GetNextDayStr(dateStart, i)
			dataItem.Date = date
			//获取潮汐数据
			var tide models.TwcTide
			ctl.db.Where("date = ? AND station = ?", date, k).First(&tide)
			//获取潮汐数据
			if tide.Id != 0 {
				tide.TideHigh1 = tools.Float64ToFixedStrZ(tools.StrToFloat64(tide.TideHigh1), 0, "0")
				tide.TideHigh2 = tools.Float64ToFixedStrZ(tools.StrToFloat64(tide.TideHigh2), 0, "0")
				tide.TideLow1 = tools.Float64ToFixedStrZ(tools.StrToFloat64(tide.TideLow1), 0, "0")
				tide.TideLow2 = tools.Float64ToFixedStrZ(tools.StrToFloat64(tide.TideLow2), 0, "0")
				dataItem.TideHighLow = tide
			}

			var tideHour1 []models.TideHour
			ctl.db.Where("date = ? AND station = ?", date, k).Find(&tideHour1)
			for _, tideHourItem := range tideHour1 {
				dataItem.TideHour = append(dataItem.TideHour, GangkouTide{
					Time: tideHourItem.Time,
					Tide: tools.Float64ToFixedStrZ(tools.StrToFloat64(tideHourItem.Tide)*100+164+v, 0, "0"),
				})
			}
			gangkou.Data = append(gangkou.Data, dataItem)
		}
		//gangkou.Data = append(gangkou.Data, data)
		gangkouData = append(gangkouData, gangkou)
		//gangkou.Data = append(gangkou.Data, struct {
		//	Date        string        `json:"date"`
		//	TideHighLow []GangkouTide `json:"tideHighLow"`
		//	TideHour    []GangkouTide `json:"tideHour"`
		//}{Date: dateStart, TideHighLow: tideHighLow, TideHour: tideHour})
		//gangkouData = append(gangkouData, gangkou)
	}
	c.JSON(200, gin.H{
		"code":    200,
		"message": "Success",
		"data":    gangkouData,
	})
}
